-- |
-- Module:        Hexena.Math.Vector
-- Stability:     Experimental
-- Portability:   Portable
--
-- Vector Calculations
module Hexena.Math.Vector 
              ( vDistance
              , vDistances
              , vDistancesTotal
              , getAllDistances
              , getDistanceTotal
              ) where

import Data.List
import Data.Packed.Vector
import Data.Packed.Matrix
import Numeric.LinearAlgebra.Util (norm)


vDistance :: Vector Double -> Vector Double -> Double
vDistance x = norm . zipVectorWith (-) x

vDistances :: [Vector Double] -> [Double]
vDistances [x,y] = [vDistance x y]
vDistances (x:z@(y:_)) = vDistance x y : vDistances z

vDistancesTotal :: [Vector Double] -> Double
vDistancesTotal = sum . vDistances

getAllDistances :: [Vector Double] -> [(Double, [Vector Double])]
getAllDistances xs = let xs' = permutations xs
                         dists = map vDistancesTotal xs'
                     in zip dists xs'

getDistanceTotal :: Matrix Double -> Double
getDistanceTotal = vDistancesTotal . toRows
